package com.yangfan.thread.thread_wangwenjun03;

/**
 * @description: 线程通信练习题2
 * @author: yangfan
 * @date: Created in 2020/1/26 0026 21:51
 * @modified By:
 */
public class TheadCommunicate2 {

    /**
     * 创建ABC三条线程 保证A线程 打印 我 B线程 打印 爱  C线程 打印 你  循环10次
     */

    private int mark = 0;//标记  0：我，1：爱，2：你

    public synchronized void printWo(){


        for(int i = 0 ; i < 10;){

            if(mark != 0){

                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }else{

                System.out.println();
                System.out.println("我");
                mark = 1;
                i++;
                notifyAll();

            }

        }



    }


    public synchronized void printAi(){

        for(int i = 0 ; i < 10;){

            if(mark != 1){

                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }else{

                System.out.println("爱");
                mark = 2;
                i++;
                notifyAll();

            }

        }

    }


    public synchronized void printNi(){

        for(int i = 0 ; i < 10;){

            if(mark != 2){

                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }else{

                System.out.println("你");
                System.out.println();
                System.out.println("=============================================");
                mark = 0;
                i++;
                notifyAll();

            }

        }

    }


    public static void main(String[] args) {

        TheadCommunicate2 t = new TheadCommunicate2();

        new Thread(){

            @Override
            public void run() {
                t.printWo();
            }

        }.start();

        new Thread(){

            @Override
            public void run() {
                t.printAi();
            }

        }.start();

        new Thread(){

            @Override
            public void run() {
                t.printNi();
            }

        }.start();


    }


}
